#encoding=utf8
from nltk import *
from collections import Counter
import ClearStopWord

#解决中文显示问题
import matplotlib
'''首先进入C:/Windows/Fonts找到简体中文的字体包，然后复制到Anaconda的目录
/Lib/site-packages/matplotlib/mpl-data/fonts/ttf下
'''
matplotlib.rcParams['font.sans-serif'] = 'SimHei'

#利用nltk进行词频特征统计
def nltk_analyse(words):
	fd = FreqDist(words)
	print(fd.keys(),'\n',fd.values())

	test_word = '训练'
	#计算出现频率和次数
	print(test_word,'出现频率：',fd.freq(test_word))
	print(test_word,'出现次数：',fd[test_word])

	#频率分布表,前十个
	fd.tabulate(10)

	#可视化词频,频率分布图
	fd.plot(30)

def counter_analyse(words):
	ct = Counter(words)
	print(ct.keys(),'\n',ct.values())

	#查找出对应字符长度的词语
	word_list = [i for i in ct if len(i) == 4]
	print(word_list)

#选择高低词频的词,根据low和height的值决定词频范围
def hl_fredword(words,low,height):
	res_list = []
	fdist = FreqDist(words)
	for word in fdist:
		if fdist.get(word) > low and fdist.get(word) < height:
			res_list.append(word+':'+str(fdist.get(word)))

	return res_list

if __name__ == '__main__':
	path = r'../CSCMNews/体育/0.txt'
	txt = ClearStopWord.read_file(path)
	words = ClearStopWord.clear_for_cut(txt)
	#nltk_analyse(words)
	#counter_analyse(words)
	print(hl_fredword(words,2,8))